package com.tms.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tms.domain.ProductLine;
import com.tms.domain.UseRecord;
import com.tms.domain.index.UseHistory;
import com.tms.mapper.develop.UseRecordMapper;
import com.tms.mapper.staff.StaffMapper;
import com.tms.service.UseRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 进出库记录业务实现
 *
 * @author ozj
 */
@Service
public class UseRecordServiceImpl implements UseRecordService {

    @Autowired
    private UseRecordMapper useRecordMapper;

    @Autowired
    private StaffMapper staffMapper;

    /**
     * 通过夹具id查询该夹具使用中的产线
     *
     * @param toolId 夹具id
     * @return
     */
    @Override
    public ProductLine findLastRecordForToolId(int toolId) {
        UseRecord useRecord = useRecordMapper.selectProductLineLastForToolId(toolId);
        return useRecord.getProductLine();
    }

    @Override
    @Cacheable(cacheNames = "useRecords", key = "#id", unless = "#result == null")
    public UseRecord findUseRecordById(Integer id) {
        UseRecord useRecord = useRecordMapper.selectByPrimaryKey(id);
        if (useRecord != null) {
            useRecord.setStaff(staffMapper.selectById(useRecord.getStaff().getId()));
        }
        return useRecord;
    }

    @Override
    @Caching(
            evict = {
                    @CacheEvict(cacheNames = "useRecordList", allEntries = true, beforeInvocation = true),
                    @CacheEvict(cacheNames = "allRecords", allEntries = true, beforeInvocation = true),
                    @CacheEvict(cacheNames = "todayRecords", allEntries = true, beforeInvocation = true),
                    @CacheEvict(cacheNames = "useHistory", allEntries = true, beforeInvocation = true)
            },
            put = {
                    @CachePut(cacheNames = "useRecords", key = "#result.id", unless = "#result == null || #result.id == 0")
            }
    )
    public UseRecord createUseRecord(UseRecord record) {
        useRecordMapper.insert(record);
        return record;
    }

    @Override
    @Caching(
            evict = {
                    @CacheEvict(cacheNames = "useRecords", key = "#id"),
                    @CacheEvict(cacheNames = "useRecordList", allEntries = true, beforeInvocation = true),
                    @CacheEvict(cacheNames = "allRecords", allEntries = true, beforeInvocation = true),
                    @CacheEvict(cacheNames = "todayRecords", allEntries = true, beforeInvocation = true),
                    @CacheEvict(cacheNames = "useHistory", allEntries = true, beforeInvocation = true)
            }

    )
    public void deleteUseRecordById(Integer id) {
        useRecordMapper.deleteByPrimaryKey(id);
    }

    @Override
    @Caching(
            evict = {
                    @CacheEvict(cacheNames = "useRecordList", allEntries = true, beforeInvocation = true),
                    @CacheEvict(cacheNames = "allRecords", allEntries = true, beforeInvocation = true),
                    @CacheEvict(cacheNames = "todayRecords", allEntries = true, beforeInvocation = true),
                    @CacheEvict(cacheNames = "useHistory", allEntries = true, beforeInvocation = true)
            },
            put = {
                    @CachePut(cacheNames = "useRecords", key = "#useRecord.id")
            }
    )
    public UseRecord updateUseRecordById(UseRecord useRecord) {
        int result = useRecordMapper.updateByPrimaryKeySelective(useRecord);
        if (result == 0) {
            throw new RuntimeException("更新失败，更新结果数为0");
        }
        return useRecord;
    }

    @Override
    @Cacheable(cacheNames = "useRecordList", key = "#page + #pageSize + #status+ #workcellId", unless = "#result == null")
    public PageInfo<UseRecord> findAllUseRecords(int page, int pageSize, int status, int workcellId) {
        PageHelper.startPage(page, pageSize);
        List<UseRecord> ans = useRecordMapper.selectAll(status, workcellId);
        for (UseRecord useRecord : ans) {
            useRecord.setStaff(staffMapper.selectById(useRecord.getStaff().getId()));
        }
        return new PageInfo<>(ans);
    }

    @Override
    @Cacheable(cacheNames = "allRecords", key = "'findAll'", unless = "#result == 0")
    public int findAllUseRecordNums() {
        return useRecordMapper.selectAllRecord();
    }


    @Override
    @Cacheable(cacheNames = "todayRecords", key = "'findAllToday'", unless = "#result == 0")
    public int findAllUseRecordNumsToday() {
        return useRecordMapper.selectRecordToday();
    }


    @Override
    @Cacheable(cacheNames = "useHistory", key = "'getUseHistory'", unless = "#result == null")
    public List<UseHistory> getUseHistory() {
        return useRecordMapper.selectMonthNum();
    }

    @Override
    public PageInfo<UseRecord> findAllOutUseRecords(int page, int pageSize, int workcellId) {
        PageHelper.startPage(page, pageSize);
        List<UseRecord> ans = useRecordMapper.selectAllOut(workcellId);
        for (UseRecord useRecord : ans) {
            useRecord.setStaff(staffMapper.selectById(useRecord.getStaff().getId()));
        }
        return new PageInfo<>(ans);
    }

    @Override
    public UseRecord findUseRecordByToolId(int id) {
        return useRecordMapper.selectProductLineLastForToolId(id);
    }
}